<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/6 0006
 * Time: 上午 9:55
 */
namespace Home\Controller;

use Think\Controller;

class AgenOrderController extends BaseController{

    /*物流*/
    public function logistics(){
        $data = M("logistics")->select();
        return $this -> ajaxSuccess($data);
    }

    /*订单、下单发货*/
    //flg:1 会员订单 2 代理下单
    public function onlineorderReceiveInfo(){
        $id = I('post.id');
        $flg = I('post.flg');
        if($flg == 1){
            $order = M("onlineorder")
                ->alias('o')
                ->join('join region r on r.id = o.regionId')
                ->where(array("o.id"=>$id,"o.status"=>2))
                ->field('o.*,r.name_path')
                ->find();
        } elseif ($flg == 2){
            $order = M("agenorder")
                ->alias('o')
                ->join('join region r on r.id = o.regionId')
                ->where(array("o.id"=>$id,"o.status"=>2))
                ->field('o.*,r.name_path')
                ->find();
        }

        if(empty($order)){
            $this->ajaxError("订单不存在！");
        }
        $logistics = M("logistics")->select();
        return $this -> ajaxSuccess(array('order'=>$order,'logistics'=>$logistics));
    }

    /*发货处理*/
    public function deliver(){
        $id = I("id");  //获取订单id
        $code = I("code"); //物流公司编码
        $logistics = I("logistics"); //物流单号
        $agenid = parent::get('agen')['id'];
        if(empty($agenid)){
            $this->ajaxError("未登录！");
        }

        if(empty($code)){
            return $this -> ajaxError("请选择物流公司！");
        }

        if(empty($logistics)){
            return $this -> ajaxError("物流单号不能为空！");
        }

        $data = array(
            "status" => "3",
            "logisticsCom"   => $code,
            "logistics"  => $logistics
        );
        $res = M("onlineOrder")
            ->where(array("id"=>$id,'toagenId'=>$agenid))
            ->save($data);
        if($res){
            return $this -> ajaxSuccess("发货成功！");
        }else{
            return $this -> ajaxError("发货失败！");
        }
    }

    //代理订单详情
    public function orderDetail(){
        //订单id
        $id = I('post.id');
        $flg = I('post.flg');
        // flg : 1 自己 2 下级
        $agenid = parent::get('agen')['id'];
        if(empty($agenid)){
            $this->ajaxError("未登录！");
        }
        if($flg == 1){
            $where = array('o.id'=>$id,'o.SourceId'=>$agenid);
        } elseif($flg == 2){
            $where = array('o.id'=>$id,'o.toagenId'=>$agenid);
        }
        $order = M('agenorder')
            ->alias('o')
            ->join('LEFT JOIN agen m on m.id = o.SourceId')
            ->join('LEFT JOIN agen a on a.id = o.toagenId')
            ->join('LEFT JOIN logistics l on l.code = o.logisticsCom')
            ->join('LEFT JOIN region r on r.id = o.regionId')
            ->join('LEFT JOIN product p on p.id = o.productId')
            ->where($where)
            ->field('o.*,m.account fromaccount,m.name fromname,a.account toaccount,a.name toname,l.name logstname,r.name_path namepath,p.name productname')
            ->find();
        if(empty($order)){
            $this->ajaxError("订单不存在");
        }
        $this->ajaxSuccess($order);
    }
}